﻿using UnityEngine;
using UnityEngine.UI;
using XLua;

namespace XLua_Ki
{
    /// 专门用于处理UI
    public class MinikimmyLuaBehavoiur : MonoBehaviour
    {
        //调用xxxPanel.lua.txt的Awake方法.
        public void OnAwake(string prefabName)
        {
            LuaUtil.CallLuaPathAction(GameManager_Ki.Instance.LuaEnv, prefabName + ".Awake", gameObject);
        }

        //绑定点击事件.
        public void AddClick(GameObject gameObject, LuaFunction luaFunction)
        {
            //Lua的绑定方法里接收一个obj.即该button的gameObject
            gameObject.GetComponent<Button>().onClick.AddListener(() => luaFunction.Call(gameObject));
        }

        //设置RectTranform的x,y,w,h的大小.
        public void SetRectTransfrom(GameObject go, float x, float y, float w, float h)
        {
            var rect = go.GetComponent<RectTransform>();
            rect.offsetMax = new Vector2(-w, -y);
            rect.offsetMin = new Vector2(x, h);
        }

        //设置Text内容.
        public void SetTextValue(GameObject go, string text)
        {
            go.GetComponent<Text>().text = text;
        }

        //设置Image图片.
        public void SetSourceImage(GameObject go, Texture2D texture)
        {
            go.GetComponent<Image>().sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
        }

        //设置父物体.
        public void SetParent(GameObject parent)
        {
            transform.SetParent(parent.transform);
        }

    }
}